/**
 * 应用状态管理类
 * 负责管理应用的整体状态
 */
class AppState {
    constructor() {
        this.currentTab = 'home';
        this.apiKey = null;
        this.isTaskExecuting = false;
        this.currentTaskId = null;
    }

    async init() {
        await this.loadSavedApiKey();
        await this.checkStatus();
    }

    async loadSavedApiKey() {
        try {
            const result = await apiService.loadApiKey();
            if (result.success && result.apiKey) {
                this.apiKey = result.apiKey;
                const apiKeyInput = document.getElementById('api-key-input');
                if (apiKeyInput) {
                    apiKeyInput.value = result.apiKey;
                }
            }
        } catch (error) {
            console.error('加载 API Key 失败:', error);
        }
    }

    async checkStatus() {
        try {
            // 检查 Claude Code 状态
            const claudeResult = await apiService.checkClaudeCode();
            const claudeStatus = document.getElementById('claude-status');
            if (claudeStatus) {
                claudeStatus.textContent = claudeResult.installed ? '已安装' : '未安装';
            }

            // 更新 API Key 状态显示
            const apiKeyStatus = document.getElementById('api-key-status');
            if (apiKeyStatus) {
                apiKeyStatus.textContent = this.apiKey ? '已配置' : '未配置';
            }
        } catch (error) {
            console.error('检查状态失败:', error);
        }
    }

    async getClaudeCodeInfo() {
        try {
            const result = await apiService.getClaudeCodeInfo();
            if (result.success) {
                return result.info;
            }
            return null;
        } catch (error) {
            console.error('获取 Claude Code 信息失败:', error);
            return null;
        }
    }

    async getClaudeCodeVersion() {
        try {
            const result = await apiService.getClaudeCodeVersion();
            if (result.success) {
                return result.version;
            }
            return null;
        } catch (error) {
            console.error('获取 Claude Code 版本失败:', error);
            return null;
        }
    }
}

// 导出到全局
window.AppState = AppState;
